package org.i_plus.model.services;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.i_plus.model.Blog_ReplyBean;
import org.i_plus.model.MemberBean;
import org.i_plus.model.dao.BlogDAOHibernate;
import org.i_plus.model.dao.Blog_ReplyHibernateDAO;
import org.i_plus.model.dao.MemberDAOHibernate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BlogReplyService {
	private Blog_ReplyHibernateDAO brdao;
	private BlogDAOHibernate bdao;
	private MemberDAOHibernate memberDAO;
	 
	public void setMemberDAO(MemberDAOHibernate memberDAO) {
		this.memberDAO = memberDAO;
	}
	public BlogReplyService(Blog_ReplyHibernateDAO brdao){
		this.brdao=brdao;
	}
	public void setBlogDAO(BlogDAOHibernate bdao){
		this.bdao=bdao;
	}
	
	public List<Blog_ReplyBean> selectById(Blog_ReplyBean bean){
		List<Blog_ReplyBean> result = null;
		if(bean!=null && bean.getRe_B_num()!=0){
			Blog_ReplyBean temp=brdao.select(bean.getRe_B_num());
			MemberBean mbean= memberDAO.Select_By_Account(temp.getMemberBean().getAccount());
			mbean.setPic1(null);
			mbean.setPic2(null);
			temp.setMemberBean(mbean);
			if(temp!=null){
				result = new ArrayList<Blog_ReplyBean>();
				result.add(temp);
			}else{
				result=brdao.select();
			}
		}
		return result;
	}
	
	public List<Blog_ReplyBean> selectByBNum(Blog_ReplyBean bean){
		List<Blog_ReplyBean> result=null;
		if(bean!=null && bean.getBlogBean().getB_num()!=0){
			result = brdao.selectByBNum(bean.getBlogBean().getB_num());
			for (Blog_ReplyBean beans:result ){
				MemberBean member = memberDAO.Select_By_Account(beans.getMemberBean().getAccount());
				member.setPic1(null);
				member.setPic2(null);
				beans.setMemberBean(member);

			}
			
		}
		return result;
	}
	
	public Blog_ReplyBean insertRe(Blog_ReplyBean bean){
		Blog_ReplyBean result= null;
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		bean.setRe_B_Time(timestamp);
		MemberBean member=memberDAO.Select_By_Account(bean.getMemberBean().getAccount());
		boolean check = this.chcekVipByAccount(bean);
		int mlike = member.getMlike();
			if(bean!=null){
				if(!check){
					if(mlike<3){
						bean.setMemberBean(member);
						brdao.insertBlog_Reply(bean.getBlogBean().getB_num(), bean.getMemberBean().getAccount(), bean.getRe_B_content(), bean.getRe_B_Time());
						mlike = mlike+1;
						member.setMlike(mlike);
						bean.setMemberBean(member);
//						memberDAO.Update(bean.getMemberBean());
						memberDAO.Update(bean.getMemberBean().getAccount(), bean.getMemberBean().getPwd(), bean.getMemberBean().getName()
								, bean.getMemberBean().getNickname(), bean.getMemberBean().getPic1(), bean.getMemberBean().getPic2(), bean.getMemberBean().getSex()
								, bean.getMemberBean().getBdate(), bean.getMemberBean().getHight(), bean.getMemberBean().getRegion(), bean.getMemberBean().getBlood()
								, bean.getMemberBean().getState(), bean.getMemberBean().getPop(), bean.getMemberBean().getBonus(), bean.getMemberBean().getHobby()
								, bean.getMemberBean().getMlike(), bean.getMemberBean().getPlay(), bean.getMemberBean().getTarot(), bean.getMemberBean().getMkind(), bean.getMemberBean().getCheckvip());
						
						result=bean;
					}else{
						return null;
					}
				}else{
					bean.setMemberBean(member);
					brdao.insertBlog_Reply(bean.getBlogBean().getB_num(), bean.getMemberBean().getAccount(), bean.getRe_B_content(), bean.getRe_B_Time());
					mlike = mlike+1;
					member.setMlike(mlike);
					bean.setMemberBean(member);
//					memberDAO.Update(bean.getMemberBean());
					memberDAO.Update(bean.getMemberBean().getAccount(), bean.getMemberBean().getPwd(), bean.getMemberBean().getName()
							, bean.getMemberBean().getNickname(), bean.getMemberBean().getPic1(), bean.getMemberBean().getPic2(), bean.getMemberBean().getSex()
							, bean.getMemberBean().getBdate(), bean.getMemberBean().getHight(), bean.getMemberBean().getRegion(), bean.getMemberBean().getBlood()
							, bean.getMemberBean().getState(), bean.getMemberBean().getPop(), bean.getMemberBean().getBonus(), bean.getMemberBean().getHobby()
							, bean.getMemberBean().getMlike(), bean.getMemberBean().getPlay(), bean.getMemberBean().getTarot(), bean.getMemberBean().getMkind(), bean.getMemberBean().getCheckvip());
					result=bean;
					}
				}
			return result;
			}
		
	
	
	public Blog_ReplyBean updateRe(Blog_ReplyBean bean){
		Blog_ReplyBean result = null;
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		bean.setRe_B_Time(timestamp);
		if(bean!=null){
			brdao.update(bean.getBlogBean().getB_num(), bean.getRe_B_num(), bean.getMemberBean().getAccount(), bean.getRe_B_content(),bean.getRe_B_Time());
			result=bean;
		}
		return result;
	}
	
	public boolean deleteRe(Blog_ReplyBean bean){
		boolean result = false;
		MemberBean member = memberDAO.Select_By_Account(bean.getMemberBean().getAccount());
		int mlike = member.getMlike();
		if(bean!=null){
			brdao.delete(bean.getRe_B_num());
			mlike=mlike-1;
			member.setMlike(mlike);
			bean.setMemberBean(member);
//			memberDAO.Update(bean.getMemberBean());
			memberDAO.Update(bean.getMemberBean().getAccount(), bean.getMemberBean().getPwd(), bean.getMemberBean().getName()
					, bean.getMemberBean().getNickname(), bean.getMemberBean().getPic1(), bean.getMemberBean().getPic2(), bean.getMemberBean().getSex()
					, bean.getMemberBean().getBdate(), bean.getMemberBean().getHight(), bean.getMemberBean().getRegion(), bean.getMemberBean().getBlood()
					, bean.getMemberBean().getState(), bean.getMemberBean().getPop(), bean.getMemberBean().getBonus(), bean.getMemberBean().getHobby()
					, bean.getMemberBean().getMlike(), bean.getMemberBean().getPlay(), bean.getMemberBean().getTarot(), bean.getMemberBean().getMkind(), bean.getMemberBean().getCheckvip());
			if(brdao.select(bean.getRe_B_num())==null){
				result=true;
			}
		}
		return result;
	}
	
	public boolean chcekVipByAccount(Blog_ReplyBean bean){
		boolean result = false;
		if(bean!=null){
			MemberBean member = memberDAO.Select_By_Account(bean.getMemberBean().getAccount());
			Date time = new Date();
			Date now = new Date(time.getTime());
			System.out.println("member="+member);
			java.sql.Date date = member.getCheckvip();
			result=date.after(now);
		}
		return result;
	}
	
	
	public static void main(String[] args){
		ApplicationContext context =
				new ClassPathXmlApplicationContext("beans.config.xml");
		SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		BlogReplyService service = (BlogReplyService) context.getBean("blog_ReplyService");
//		Blog_ReplyBean blog = new Blog_ReplyBean();
//		BlogBean blogbean = new BlogBean();
//		blogbean.setB_num(18);
//		blog.setBlogBean(blogbean);
//		List<Blog_ReplyBean> list =  service.selectByBNum(blog);
//		for(Blog_ReplyBean temp:list)
//		System.out.println(temp.getMemberBean().getName());
		
//		Blog_ReplyBean bean= new Blog_ReplyBean();
//		BlogBean bbean = new BlogBean();
//		MemberBean member = new MemberBean();
//		bbean.setB_num(20);
//		bean.setBlogBean(bbean);
//		bean.setRe_B_content("reply3");
//		member.setAccount("456");
//		bean.setMemberBean(member);
//		service.insertRe(bean);
//		List<Blog_ReplyBean> beans =service.selectById("456");
//		for(Blog_ReplyBean bean:beans){
//			System.out.println(beans);
//		}
//		int re_b_num=36;
//		Blog_ReplyBean bean = (Blog_ReplyBean)session.get(Blog_ReplyBean.class, re_b_num);
//		bean.setRe_B_content("replychange");
//		service.updateRe(bean);
//		System.out.println("done");
//		Blog_ReplyBean bean = (Blog_ReplyBean)session.get(Blog_ReplyBean.class, re_b_num);
//		service.deleteRe(bean);
		
//		String account = "456";
//		MemberBean member = new MemberBean();
//		member.setAccount(account);
//		Blog_ReplyBean reply = new Blog_ReplyBean();
//		reply.setMemberBean(member);
//		System.out.println("你的VIP判定結果為" + service.chcekVipByAccount(reply));
		
		
		session.getTransaction().commit();
	}
}
